/*
 * DictionaryRecord.java
 *
 * Copyright(c) 2011
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.gerengdic.dictionary;

import de.studiorutton.gerengdic.dictionary.records.LeftRightValuesRecord;
import edu.sdsu.util.Comparer;

public abstract class DictionaryRecord {
	private final static String TAG = "learningwords";

	public final static int TYPE_HEADLINE = 1;
	public final static int TYPE_CONTENT = 2;

	private final static int FALLBACK_TO_RECORD_COMPARER = -10;
	private final static int GREATER_THAN = 1;
	private final static int EQUALS = 0;
	private final static int LESS_THAN = -1;

	public int type;
	public int errors;
	
	static DComparer dComparer = new DComparer();
	
	public DictionaryRecord( int type, int errors ) {
		this.type = type;
		this.errors = errors;
	}
	
	public LeftRightValuesRecord getLrvr() {
		return null;
	}
	
	static class DComparer implements Comparer<DictionaryRecord> {
		
		
		public boolean greaterThan(DictionaryRecord elem,
				DictionaryRecord object) {
			int result = compare(elem,object);
			if ( result == FALLBACK_TO_RECORD_COMPARER )
				return ((DictionaryRecordContent)elem).greaterThan( (DictionaryRecordContent)object );
			else
				return result == GREATER_THAN;
		}

		public boolean lessThan(DictionaryRecord elem, DictionaryRecord object) {
			int result = compare(elem,object);
			if ( result == FALLBACK_TO_RECORD_COMPARER )
				return ((DictionaryRecordContent)elem).lessThan( (DictionaryRecordContent)object );
			else
				return result == LESS_THAN;			
		}

		public boolean equals(DictionaryRecord elem, DictionaryRecord object) {
			int result = compare(elem,object);
			if( result == FALLBACK_TO_RECORD_COMPARER )
				return ((DictionaryRecordContent)elem).equals( (DictionaryRecordContent)object );
			else
				return result == EQUALS;			
		}
		
		public int compare( DictionaryRecord arg1, DictionaryRecord arg2 ) {
			if ( arg1.type == arg2.type  ) {
				if (arg1.errors < arg2.errors)
					return LESS_THAN;
				else if (arg1.errors > arg2.errors)
					return GREATER_THAN;
				else
					if ( arg1.type == TYPE_CONTENT ) {
						return FALLBACK_TO_RECORD_COMPARER;
					} else
						return EQUALS;
			} else {
				if ( arg1.type == TYPE_HEADLINE && arg2.type == TYPE_CONTENT ) {
//					Log.d(TAG, "Comparing headline(" + arg1.errors +") and content(" + arg2.errors + " - " + arg2.indexAll.indexRecord.getKey() + ")");
					return compareHeadlineAndContent( arg1, arg2 );
				}
				else {
//					Log.d(TAG, "Comparing content(" + arg1.errors + " - " + arg1.indexAll.indexRecord.getKey() + ") and headline(" + arg1.errors +")");
					return compareContentAndHeadline( arg1, arg2 );
				}
			}
		}
		
		public int compareHeadlineAndContent( DictionaryRecord arg1, DictionaryRecord arg2 ) {
			if ( arg1.errors <= arg2.errors )
				return LESS_THAN;
			else
				return GREATER_THAN;
		}
		
		public int compareContentAndHeadline( DictionaryRecord arg1, DictionaryRecord arg2 ) {
			if ( arg1.errors < arg2.errors )
				return LESS_THAN;
			else
				return GREATER_THAN;
		}
		
	}
		
}
